# -*- coding: utf-8 -*-
import pymongo
client = pymongo.MongoClient('127.0.0.1')
db = client.city4
import re
import MySQLdb
import MySQLdb.cursors
conn = MySQLdb.connect("127.0.0.1","root","123456789", "shantianjun",charset="utf8mb4",cursorclass=MySQLdb.cursors.DictCursor)
sql = "select * from girl"
cursor = conn.cursor()
cursor.execute(sql)
girl = cursor.fetchallDict()
data = {}
urlpool = set()
dd = []
girls = {}
for x in girl:
    girls.update({x["url"]:(x["id"],x["sid"])})
# import ipdb;ipdb.set_trace()
urlpool = set()
for y in db.diary.find():
        data = {}
        if y["url"]+y["time"] in urlpool:
            print "continue"+y["url"]
            continue
        if not y.get("girlUrl"):
            # import ipdb;ipdb.set_trace()
            continue
        url = y["girlUrl"].replace("https://www.cityheaven.net","")
        if not girls.get(url):
            print "not gid"
            # import ipdb;ipdb.set_trace()
            continue
        data["gid"],data["sid"] = girls.get(url)
        data["name"] = y["name"]
        data["title"] = y["title"]
        data["context"] = y["context"]
        # if y.get("image_urls"):
        #     result = clent.dmm.img.find_one({"type":"citydiary","source":y.get("image_urls")})
        #     if result:
        #         data["thumb"] = result.get("path")
        data["thumb"] = y.get("image_urls")
        if not data["thumb"]:
            print "not img"+y["url"]
        # if type(data["thumb"]) == list:
            # data["thumb"] = "".join(data["thumb"])
        fields = tuple(data.keys())
        fields = str(tuple(fields)).replace("'",'')
        data_dict = data.values()
        data_values = "(" + "%s," * (len(data_dict)) + ")"
        data_values = data_values.replace(',)', ')')

        sql = "insert into diary %s values %s" % (fields,data_values)
        dd.append(tuple(data_dict))
        urlpool.add(y["url"]+y["time"])
        print sql

cursor.executemany(sql,dd)
cursor.close()
conn.commit()
# insert info cate (name) select select distinct(cate) from stores where cate <> ""
